package com.zhuiyi.yiqingbackend.service.impl;

import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.domain.Base;
import com.zhuiyi.yiqingbackend.domain.SafeBase;
import com.zhuiyi.yiqingbackend.domain.Touch;
import com.zhuiyi.yiqingbackend.domain.request.AddTouchRequest;
import com.zhuiyi.yiqingbackend.domain.request.UpdateTouchRequest;
import com.zhuiyi.yiqingbackend.domain.response.TouchResponse;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.mapper.BaseMapper;
import com.zhuiyi.yiqingbackend.mapper.TouchMapper;
import com.zhuiyi.yiqingbackend.service.BaseService;
import com.zhuiyi.yiqingbackend.service.TouchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class TouchServiceImpl implements TouchService {

    @Resource
    private TouchMapper touchMapper;
    @Resource
    private BaseMapper baseMapper;
    @Resource
    private BaseService baseService;


    @Override
    public List<TouchResponse> getAllTouch() {
        List<Touch> allTouch = touchMapper.getAllTouch();
        List<TouchResponse> result = new ArrayList<>();
        for(int i=0;i<allTouch.size();++i){
            int baseId = allTouch.get(i).getBaseId();
            Base base = baseService.getBaseById(baseId);
            SafeBase safeBase = new SafeBase(base);
            result.add(new TouchResponse(safeBase, allTouch.get(i)));
        }
        return result;
    }

    @Override
    public List<TouchResponse> getTouchById(int id) {
        Touch touch = touchMapper.getTouchById(id);
        List<TouchResponse> result = new ArrayList<>();
        Base base = baseService.getBaseById(touch.getBaseId());
        SafeBase safeBase = new SafeBase(base);
        result.add(new TouchResponse(safeBase,touch));
        return result;
    }

    @Override
    public boolean addTouch(AddTouchRequest request) {
        String gender = request.getGender();
        String address = request.getAddress();
        String idCard = request.getIdCard();
        String desc = request.getDesc();//症状
        String resource = request.getResource();//感染源
        String name = request.getName();
        Date time = request.getTime();//开始隔离时间
        String phone = request.getPhone();
        if(StringUtils.isEmpty(phone)||StringUtils.isBlank(phone)){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"电话号码为空");
        }
        Base base = baseMapper.getBaseByPhone(phone);
        if(base==null){
            baseMapper.addBase(new Base(name,idCard,phone,gender));
            int baseId = baseMapper.getBaseByPhone(phone).getId();
            Touch touch = new Touch(baseId,resource,address,time,desc);
            return touchMapper.addTouch(touch);
        }else {
            int baseId = base.getId();
            return touchMapper.addTouch(new Touch(baseId,resource,address,time,desc));
        }
    }

    @Override
    public boolean deleteTouchById(int id) {

        boolean result = touchMapper.deleteTouchById(id);
        return result;
    }

    @Override
    public boolean updateTouch(UpdateTouchRequest request) {
        int baseId = request.getBaseId();
        String gender = request.getGender();
        String idCard = request.getIdCard();
        String uname = request.getUname();

        Base base = baseService.getBaseById(baseId);
        base.setGender(gender);
        base.setIdCard(idCard);
        base.setUname(uname);
        boolean result1 = baseService.updateBase(base);

        int id = request.getTouchId();
        String finished = request.getFinished();
        String comeFrom = request.getComeFrom();
        String isoAddress = request.getIsoAddress();
        Date startDate = request.getStartDate();
        String desc = request.getDesc();
        Touch touch = new Touch(id,baseId, comeFrom, isoAddress, startDate, finished,desc);
        boolean result2 = touchMapper.updateTouch(touch);
        return result1 && result2;
    }
}
